<?php

function addMessage($mss){
	if(!isset($_SESSION['messages'])){
		$_SESSION['messages']=array();
	}
	$_SESSION['messages'][]=$mss;
}

function randomString($n=8){
	$string="";
	$lett=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0");
	for($v=0;$v<$n;$v++){
		$string.=$lett[ rand(0,count($lett)) ];
	}
	return $string;
}
function get_filename($fname){
	$t=correct_slashes($fname);
	$slash=strpos($t,"/");
	if($slash !== false ){
		$cv=explode("/",$t);
		$fname=$fname=substr($cv[count($cv)-1],0,-4);
		return $fname;
	}
	return $t;
}

function correct_slashes($stx){
	$stx=str_replace("\\","/",$stx);
	return $stx;
}

function get_file_extension($url){
  $ext = substr(strrchr($url, "."), 1);
  return $ext;
}

function tfv($str){
 return str_replace('"','&quot;',from_db_text($str));
}

function dfv($str){
	//$t=secure_db_text(str_replace('&quot;','"',$str));
	$t=secure_db_text($str);
	return $t;
}


function string2Array($string,$separador){
	return explode($separador,$string);
}

function get_elementIDS($elementos){
	if(is_array($elementos)){
		$total=count($elementos);
		$ids=array();
		for($b=0; $b<$total; $b++){
			$vb=explode("-",$elementos[$b]);
			
			$ids[]=$vb[1];
		}
	}
	return $ids;
}

function uploadfile($file_name, $savePath, $newname = false){
	if ( $_FILES[$file_name]['name'] != ""){
			
			$filename=get_filename($_FILES[$file_name]['name']);
			$ext=get_file_extension( $filename );
			if($newname != false)
				$newfile = $newname.".".$ext;
			else
				$newfile = $filename.".".$ext;
				
			if(move_uploaded_file($_FILES[$file_name]['tmp_name'], $savePath.$newfile )){
				//chmod($savePath.$newfile ,0777);
				return true;
			}else{
				echo "Erro";
			}	
	}
	//print_r($_FILES);
	
	return false;
}

?>